<?php
/**
 * Eyas start
 *
 * @package   WP ENV
 * @author    Eyas <liuyuesongde@163.com>
 * @license   GPL-2.0+
 * @link      http://eyasweb.com
 * @copyright 2015-2016 Eyas
 *
 * @wordpress-plugin
 * Plugin Name: WP ENV
 * Plugin URI:  http://eyasweb.com
 * Description: 设置不同的开发环境，可以将wordpress的设置不同
 * Version:     0.0.1
 * Author:      Eyas
 * Author URI:  http://eyasweb.com
 * License:     GPL-2.0+
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain: ey
 * Domain Path: /lang
 */
define('WP_ENV_PLUGIN',plugin_dir_path( __FILE__ ));
require_once(WP_ENV_PLUGIN.'options.php');
class WP_ENV_Checkout{
  protected $setting;//[array]当前环境的设置变量
  protected $current;//[string]当前环境
  function __construct(){
    if(file_exists(WP_ENV_PLUGIN.'env/.env')){
      $defaults = parse_ini_file(WP_ENV_PLUGIN.'env/.env',true);
      $this->current = $defaults['ENV'];
      if($this->getENV($this->current)){
        $this->setting = $this->getENV($this->current);
      }else{
        $this->setting = $defaults['defaults'];
      }

    }
    // option page
    // add_action('admin_menu',array($this,'add_option_menu'));
    // add_action('admin_init',array($this,'register_my_setting'));
  }
  function getCurrentEnv(){
    return $this->current;
  }
  function getSetting(){
    return $this->setting;
  }
  // 返回环境的设置
  function getENV($envname){
    if(file_exists(WP_ENV_PLUGIN.'env/.env.'.$envname)){
      $env_setting = parse_ini_file(WP_ENV_PLUGIN.'env/.env.'.$envname,true);
      return $env_setting['settings'];
    }else{
      return false;
    }
  }
  // function env_list(){
  //
  // }
  // function add_option_menu(){
  //   add_options_page( __( 'devlement envirment settings', 'ey' ), __( 'env setting', 'ey' ), 'edit_theme_options', 'env_setting_options', array($this,'add_option_page') );
  //
  // }
  // function add_option_page($envname=''){
  //   if($_POST['type']=='list'){
  //     wp_env_setting_page_list($this->setting);
  //   }else if($_POST['type']=='edit'){
  //     wp_env_setting_page_detail($this->setting);
  //   }else if($_POST['type']=='del'){
  //     wp_env_setting_del($this->setting);
  //   }else{
  //     wp_env_setting_page_list($this->setting);
  //   }
  //
  // }
  // function register_my_setting(){
  //   register_setting( '', '', $this->save_option($_POST));
  // }
  // function save_option($input){
  //   $this->write_ini_file($input,WP_ENV_PLUGIN.'env/.env.'.$input['ENV']['name'],true);
  //   return 'save_option';
  // }
  // function write_ini_file($assoc_arr, $path, $has_sections = FALSE) {
  //   $content = "";
  //     if ($has_sections) {
  //         foreach ($assoc_arr as $key => $elem) {
  //             $content .= "[" . $key . "]\n";
  //             foreach ($elem as $key2 => $elem2) {
  //                 if (is_array($elem2)) {
  //                     for ($i = 0; $i < count($elem2); $i++) {
  //                         $content .= $key2 . "[] = \"" . $elem2[$i] . "\"\n";
  //                     }
  //                 } else if ($elem2 == "")
  //                     $content .= $key2 . " = \n";
  //                 else
  //                     $content .= $key2 . " = \"" . $elem2 . "\"\n";
  //             }
  //         }
  //     } else {
  //         foreach ($assoc_arr as $key => $elem) {
  //             if (is_array($elem)) {
  //                 for ($i = 0; $i < count($elem); $i++) {
  //                     $content .= $key2 . "[] = \"" . $elem[$i] . "\"\n";
  //                 }
  //             } else if ($elem == "")
  //                 $content .= $key2 . " = \n";
  //             else
  //                 $content .= $key2 . " = \"" . $elem . "\"\n";
  //         }
  //     }
  //
  //     if (!$handle = fopen($path, 'w')) {
  //         return false;
  //     }
  //     if (!fwrite($handle, $content)) {
  //         return false;
  //     }
  //     fclose($handle);
  //     return true;
  // }
}

function wp_env_init(){
  $myenvSetting = (new WP_ENV_Checkout())->getSetting();
  if($myenvSetting){
    foreach($myenvSetting as $key => $val){
      define($key,$val);
    }
  }
}
function ey_isProduction(){
  $current = (new WP_ENV_Checkout())->getCurrentEnv();
  return $current=='production'?true:false;
}

add_action( 'init', 'wp_env_init' );

 ?>
